/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DialogoModificarUsuario.java
 *
 * Created on 30-nov-2009, 19:05:03
 */
package dialogos;

import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import net.java.balloontip.BalloonTip;
import net.java.balloontip.styles.MinimalBalloonStyle;
import net.java.balloontip.utils.TimingUtils;
import utilidades.LeerArchivo;

/**
 *
 * @author Shidobu
 */
public class DialogoModificarUsuario extends javax.swing.JDialog {
	String usuarioActual;

	/** Creates new form DialogoModificarUsuario */
	public DialogoModificarUsuario (java.awt.Frame parent, boolean modal, String nombre) {
		super (parent, modal);
		initComponents ();
		this.setLocationRelativeTo (null);
		LeerArchivo datos = new LeerArchivo ();
		String dato = new String ();
		try {
			dato = datos.cargarDatos (nombre);
			System.out.println (dato);
		}
		catch (Exception e) {
			JOptionPane.showMessageDialog (this, "Error al intentar leer del archivo de registro de usuarios",
										   "Error en la lectura", JOptionPane.ERROR_MESSAGE);
		}
		//System.out.println (dato + " nombre " + nombre);
		String corte[];
		corte = dato.split ("#");
		jTextFieldNombre.setText (corte[0]);
		usuarioActual = corte[0];
		jPasswordFieldReClave.setText (corte[1]);
		jPasswordFieldClave.setText (corte[1]);
		jTextFieldCorreo.setText (corte[2]);
		jTextFieldTelefono.setText (corte[3]);
		jTextFieldMovil.setText (corte[4]);
	}

	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings ("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelModificarDatosUsuario = new javax.swing.JPanel();
        jLabelNombre = new javax.swing.JLabel();
        jLabelClave = new javax.swing.JLabel();
        jLabelReClave = new javax.swing.JLabel();
        jLabelCorreo = new javax.swing.JLabel();
        jLabelTelefono = new javax.swing.JLabel();
        jLabelMovil = new javax.swing.JLabel();
        jTextFieldNombre = new javax.swing.JTextField();
        jTextFieldCorreo = new javax.swing.JTextField();
        jTextFieldTelefono = new javax.swing.JTextField();
        jTextFieldMovil = new javax.swing.JTextField();
        jPasswordFieldReClave = new javax.swing.JPasswordField();
        jPasswordFieldClave = new javax.swing.JPasswordField();
        jButtonModificar = new javax.swing.JButton();
        jButtonCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("REUSA - Modificar datos usuario");

        jPanelModificarDatosUsuario.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Modicar Datos", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        jLabelNombre.setText("Nombre:");

        jLabelClave.setText("Clave:");

        jLabelReClave.setText("Repita Clave:");

        jLabelCorreo.setText("Correo Electrónico:");

        jLabelTelefono.setText("Telefono:");

        jLabelMovil.setText("Telefono Celular:");

        javax.swing.GroupLayout jPanelModificarDatosUsuarioLayout = new javax.swing.GroupLayout(jPanelModificarDatosUsuario);
        jPanelModificarDatosUsuario.setLayout(jPanelModificarDatosUsuarioLayout);
        jPanelModificarDatosUsuarioLayout.setHorizontalGroup(
            jPanelModificarDatosUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelModificarDatosUsuarioLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelModificarDatosUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelTelefono)
                    .addComponent(jLabelClave)
                    .addComponent(jLabelNombre)
                    .addComponent(jLabelMovil)
                    .addComponent(jLabelReClave)
                    .addComponent(jLabelCorreo))
                .addGap(34, 34, 34)
                .addGroup(jPanelModificarDatosUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTextFieldTelefono, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
                    .addComponent(jTextFieldCorreo, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
                    .addComponent(jPasswordFieldReClave, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
                    .addComponent(jTextFieldMovil, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
                    .addComponent(jTextFieldNombre, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
                    .addComponent(jPasswordFieldClave, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanelModificarDatosUsuarioLayout.setVerticalGroup(
            jPanelModificarDatosUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelModificarDatosUsuarioLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelModificarDatosUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelNombre)
                    .addComponent(jTextFieldNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanelModificarDatosUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelClave)
                    .addComponent(jPasswordFieldClave, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanelModificarDatosUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jPasswordFieldReClave, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelReClave))
                .addGap(18, 18, 18)
                .addGroup(jPanelModificarDatosUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldCorreo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelCorreo))
                .addGap(18, 18, 18)
                .addGroup(jPanelModificarDatosUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelTelefono))
                .addGap(21, 21, 21)
                .addGroup(jPanelModificarDatosUsuarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldMovil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelMovil))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        jButtonModificar.setText("Guardar");
        jButtonModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonModificarActionPerformed(evt);
            }
        });

        jButtonCancelar.setText("Cancelar");
        jButtonCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButtonModificar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButtonCancelar))
                    .addComponent(jPanelModificarDatosUsuario, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelModificarDatosUsuario, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonModificar)
                    .addComponent(jButtonCancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void jButtonModificarActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonModificarActionPerformed
		if (String.valueOf (jPasswordFieldReClave.getPassword ()).equals (String.valueOf (jPasswordFieldClave.getPassword ()))) {
			try {
				LeerArchivo datos = new LeerArchivo ();
				datos.guardarDatos (usuarioActual, jTextFieldNombre.getText (), String.valueOf (jPasswordFieldReClave.getPassword ()),
									jTextFieldCorreo.getText (), jTextFieldTelefono.getText (), jTextFieldMovil.getText ());
				JOptionPane.showMessageDialog (null, "Su cuenta ha sido modificada existosamente");
				this.setVisible (false);
			}
			catch (Exception ex) {
				JOptionPane.showMessageDialog (this, "Error al intentar escribir en el archivo de registro de usuarios",
											   "Error en la escritura", JOptionPane.ERROR_MESSAGE);
			}
		}
		else {
			TimingUtils.showTimedBalloon (new BalloonTip (jPasswordFieldReClave, "Las contraseñas no coinciden",
														  new MinimalBalloonStyle (new Color (231, 225, 165), 2), BalloonTip.Orientation.RIGHT_ABOVE,
														  BalloonTip.AttachLocation.ALIGNED, 20, 10, false), 3000);
		}
}//GEN-LAST:event_jButtonModificarActionPerformed

	private void jButtonCancelarActionPerformed (java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelarActionPerformed
		this.setVisible (false);
	}//GEN-LAST:event_jButtonCancelarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancelar;
    private javax.swing.JButton jButtonModificar;
    private javax.swing.JLabel jLabelClave;
    private javax.swing.JLabel jLabelCorreo;
    private javax.swing.JLabel jLabelMovil;
    private javax.swing.JLabel jLabelNombre;
    private javax.swing.JLabel jLabelReClave;
    private javax.swing.JLabel jLabelTelefono;
    private javax.swing.JPanel jPanelModificarDatosUsuario;
    private javax.swing.JPasswordField jPasswordFieldClave;
    private javax.swing.JPasswordField jPasswordFieldReClave;
    private javax.swing.JTextField jTextFieldCorreo;
    private javax.swing.JTextField jTextFieldMovil;
    private javax.swing.JTextField jTextFieldNombre;
    private javax.swing.JTextField jTextFieldTelefono;
    // End of variables declaration//GEN-END:variables
}
